package com.denso.workflow;

import com.denso.workflow.admin.image.ImageService;
import com.denso.workflow.admin.operation.OperationlogService;
import com.denso.workflow.admin.operator.OperatorService;
import com.denso.workflow.common.WorkflowConstant;
import com.denso.workflow.common.model.Image;
import com.denso.workflow.common.model.Operationlog;
import com.denso.workflow.interceptor.OperatorInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;

import java.util.List;

/**
 * Created by lijian on 2018/9/22.
 */
@Before(OperatorInterceptor.class)
public class IndexController extends Controller{

    static final OperatorService operatorService = new OperatorService();
    static final OperationlogService operationlogService = new OperationlogService();
    static final ImageService imageService = new ImageService();

    public void index() {
        int codeBegin = PropKit.getInt("codeBegin");
        int codeEnd = PropKit.getInt("codeEnd");
        setAttr("codeBegin", codeBegin);
        setAttr("codeEnd", codeEnd);

        render("index.html");
    }

    public void show() {
        Operationlog operationlog = new Operationlog();
        operationlog.setOperator((String)getSessionAttr("operator"));

        String boxCode = getPara("boxCode").trim();
        List<Image> imageList = imageService.findByCode(boxCode);
        if(imageList.size() == 0) {
            setAttr("notice", "未登记此品番 [" + boxCode + "] 指导书,请联系管理员添加");
            render("index.html");
            return;
        }
        operationlog.setBoxCode(boxCode);

        Image image = imageList.get(0);

        // 先检查是不是万能码
        String partCode = getPara("partCode").trim();
        operationlog.setFullCode(partCode); // 在fullCode中记录供应商完整扫码信息

        boolean matched = false;
        if(partCode.equals(WorkflowConstant.CODE_UNIVERSAL)) {
            matched = true;
            // 万能码
            operationlog.setPartCode(partCode);
        } else {
            int codeBegin = image.getPcodeBegin();
            int codeEnd = image.getPcodeEnd();
            if(codeBegin > partCode.length() || codeEnd > partCode.length()) {
                // 无法按要求截取
                setAttr("notice", "供应商品番[" + partCode + "]不符合录入格式要求");
                matched = false;
            } else {
                String pcode = partCode.substring(image.getPcodeBegin(), image.getPcodeEnd());
                operationlog.setPartCode(pcode);
                if (pcode.equalsIgnoreCase(boxCode)) {
                    // 匹配成功
                    matched = true;
                } else {
                    // 匹配失败
                    setAttr("notice", "供应商品番[" + pcode + "]与TFDACC品番[" + boxCode + "]不匹配,请检查后重新扫描");
                    matched = false;
                }
            }
        }

        if(matched) {
            // 匹配成功
            setAttr("image", image.getImg());
            operationlog.setResult(WorkflowConstant.LOG_SUCCESS);
            operationlogService.save(operationlog);
            render("show.html");
        } else {
            // 匹配失败
            operationlog.setResult(WorkflowConstant.LOG_ERROR);
            operationlogService.save(operationlog);
            render("index.html");
        }
    }

    // 仅清除权限判断,保留全局SessionInViewInterceptor
    @Clear(OperatorInterceptor.class)
    public void login() {
        String name = getPara("name");
        String password = getPara("password");

        boolean resFlag = operatorService.login(name, password);
        if(resFlag) {
            setSessionAttr("operator", name);
            render("index.html");
        } else {
            setAttr("notice", "登录账号认证失败,请重新输入");
            render("login.html");
        }
    }

    public void logout() {
        removeSessionAttr("operator");
        render("login.html");
    }
}
